<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>最长理想子序列</title>
</head>
<body>
  <script>
    function fn(s, k) {
      const dp = new Array(26).fill(0);
      let res = 0;
      for (const ch of s) {
        const index = ch.charCodeAt() - 'a'.charCodeAt();
        dp[index]++;
        for (let i = -k; i <= k; i++) {
          if (index + i < 0 || i === 0) continue;
          if (index + i > 25) break;
          dp[index] = Math.max(dp[index], dp[index + i] + 1); //index+i在0到25之间才转移动态方程
        }
        res = Math.max(res, dp[index])
      }
      return res
    }
    console.log(fn('acfgbd', 2))
  </script>
</body>
</html>